#pragma once
#include "BaseInc.h"

class Utility
{
public:
	Utility() {}
	~Utility() {}
	static std::wstring Utf82Unicode(const std::string& utf8string)
	{
		int widesize = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, NULL, 0);
		if (widesize == ERROR_NO_UNICODE_TRANSLATION)
		{
			throw std::exception("Invalid UTF-8 sequence.");
		}
		if (widesize == 0)
		{
			throw std::exception("Error in conversion.");
		}

		std::vector<wchar_t> resultstring(widesize);

		int convresult = ::MultiByteToWideChar(CP_UTF8, 0, utf8string.c_str(), -1, &resultstring[0], widesize);

		if (convresult != widesize)
		{
			throw std::exception("La falla!");
		}

		return std::wstring(&resultstring[0]);
	}

	static std::wstring ConvertToWstring(std::string content)
	{
		int len = content.size();
		wchar_t* wch = new wchar_t[len + 1];
		memset(wch, 0, sizeof(wchar_t) * (len + 1));
		mbstowcs_s(nullptr, wch, len + 1, content.data(), len);

		std:wstring cvtStr = wch;
		delete[]wch;
		return cvtStr;
	}
	static std::string ConvertTostring(std::wstring content)
	{
		int len = content.size();
		char* ch = new char[len + 1];
		memset(ch, 0, sizeof(char) * (len + 1));
		wcstombs_s(nullptr, ch, len+1, content.data(), len);
		std:string cvtStr = ch;
		delete[]ch;
		return cvtStr;
	}
	static std::wstring Acsi2WideByte(std::string& strascii)
	{
		int widesize = MultiByteToWideChar(CP_ACP, 0, (char*)strascii.c_str(), -1, NULL, 0);
		if (widesize == ERROR_NO_UNICODE_TRANSLATION)
		{
			throw std::exception("Invalid UTF-8 sequence.");
		}
		if (widesize == 0)
		{
			throw std::exception("Error in conversion.");
		}
		std::vector<wchar_t> resultstring(widesize);
		int convresult = MultiByteToWideChar(CP_ACP, 0, (char*)strascii.c_str(), -1, &resultstring[0], widesize);


		if (convresult != widesize)
		{
			throw std::exception("La falla!");
		}

		return std::wstring(&resultstring[0]);
	}

	static std::string Unicode2Utf8(const std::wstring& widestring)
	{
		int utf8size = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, NULL, 0, NULL, NULL);
		if (utf8size == 0)
		{
			throw std::exception("Error in conversion.");
		}

		std::vector<char> resultstring(utf8size);

		int convresult = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, &resultstring[0], utf8size, NULL, NULL);

		if (convresult != utf8size)
		{
			throw std::exception("La falla!");
		}

		return std::string(&resultstring[0]);
	}
	/*static tstring ConvertToTstring(std::string content)
	{
#if _UNICODE
		return	ConvertToWstring(content);
#else
		return  content;
#endif
	}*/
	static std::vector<string> GetTiXmlElementAttributes(TiXmlElement* ele);
};
